<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<div class="main-content">
    <div class="box box-main">
        <form id="inputForm" model="post" action="<c:url value="/web/admin/enterprise/sytemuser/saveOrUpdate.action"/>"
              method="post" class="form-horizontal">
            <div class="box-body">
                <div class="form-unit">基本信息</div>
                <input id="userId" name="id" value="${systemUser.id}" type="hidden">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required ">*</span> 手机号：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="userAccount" name="userAccount" disabled value="${systemUser.userAccount}"
                                       maxlength="100" class="form-control"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span> 姓名：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="userName" name="userName" maxlength="100" disabled
                                       value="${systemUser.userName}"
                                       class="form-control"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span> 工号：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="userName" name="userName" maxlength="100" value="${systemUser.workNo}" disabled
                                       class="form-control"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span> 所在部门：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="userName" name="userName" maxlength="100"
                                       value="${systemUser.organization}" disabled
                                       class="form-control"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span> 所在岗位：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="userName" name="userName" maxlength="100" value="${systemUser.position}" disabled
                                       class="form-control"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span> 登录密码：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="password" name="password" id="password" type="password" maxlength="100"
                                       value="${systemUser.password}"
                                       class="form-control required "/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span> 确认密码：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="rePassword" name="rePassword" maxlength="100"
                                       value="${systemUser.password}" type="password"
                                       class="form-control required" equalto="#password"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span> 选择角色：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <select class="form-control required" name="roleId">
                                    <option value=""></option>
                                    <c:forEach items="${systemRoleList}" var="role">
                                        <option value="${role.id}" <c:if
                                            test="${role.id==myRole.id}">selected</c:if>>${role.roleName}</option>
                                    </c:forEach>
                                </select>

                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="box-footer">
                <div class="row">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i>
                            保 存
                        </button>&nbsp;

                        <button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="closeView()"><i
                                class="fa fa-times"></i> 关 闭
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    $("#inputForm").validate({
        submitHandler: function (form) {
            getEnterData();
            js.ajaxSubmitForm($(form), function (data) {
                js.showMessage(data.msg);
                if (data.status == Global.NO) {
                    closeView();
                    $('#dataGrid').dataGrid("refresh");
                }
            }, "json");
        }
    });

    var select_modal = $('#select_role').magicSuggest({
        allowFreeEntries: false,
        data: '<c:url value="/web/admin/systemRole/getSystemRoleListMagic.action?id=${systemUser.systemRole.id}" />',
        queryParam: 'q',
        valueField: "value",
        displayField: "text",
        maxSelection: 1,
        noSuggestionText: '',
        placeholder: '请选择',
        required: true,
        autoSelect: true,
        selectFirst: true,
        value: magicSuggestValue('${systemUser.systemRole.id}')
    });

    var select_enterprise = $('#select_enterprise').magicSuggest({
        allowFreeEntries: false,
        data: '<c:url value="/web/admin/enterprise/getEnterpriseListMagic.action" />',
        queryParam: 'q',
        valueField: "value",
        displayField: "text",
        maxSelection: 1,
        noSuggestionText: '',
        placeholder: '选择企业',
        required: true,
        autoSelect: true,
        selectFirst: true,
        value: magicSuggestValue('')
    });

    $('#company_').magicSuggest({
        allowFreeEntries: false,
        data: '<c:url value="/web/admin/enterprise/getEnterpriseListMagic.action" />',
        queryParam: 'q',
        valueField: "value",
        displayField: "text",
        maxSelection: 1,
        noSuggestionText: '',
        placeholder: '选择企业',
        required: true,
        autoSelect: true,
        selectFirst: true,
        value: magicSuggestValue('')
    });

    function closeView() {
        layer.close(LayWin['wSystemUser']);
    }

    function changeContent(index) {
        var _this = $("#" + index + "Content");
        hideAllContent();
        _this.removeClass("hide");
    }

    function hideAllContent() {
        if (!$("#EnterpriseContent").hasClass("hide")) {
            $("#EnterpriseContent").addClass("hide");
        }
    }

    var ordinaryKey = 1;
    $(document).ready(function () {
        addEnterprise();
    });

    function removeEnterprise(_this) {
        var input = $(_this).parent().parent().parent();
        input.remove();
        if (ordinaryKey > 1) {
            ordinaryKey--;
        }
    }

    function addEnterprise() {
        $('#EnterpriseContent').append(js.template('enterpriseTpl', {
            key: ordinaryKey
        }));
        if (ordinaryKey === 1) {
            $("#delete_" + ordinaryKey).addClass("hide");
        }
        var enterpriseId_ = $('#enterpriseId_' + ordinaryKey).magicSuggest({
            allowFreeEntries: false,
            data: '<c:url value="/web/admin/enterprise/getEnterpriseListMagic.action" />',
            queryParam: 'q',
            valueField: "value",
            displayField: "text",
            maxSelection: 1,
            noSuggestionText: '',
            placeholder: '请选择',
            required: true,
            autoSelect: true,
            selectFirst: true,
            value: magicSuggestValue('')
        });
        ordinaryKey++;
    }

    function getEnterData() {
        var data = new Array();
        var name = "enterpriseId[]";
        $("input[name='" + name + "']").each(function (j, item) {
            if (!data.contains(item.value)) {
                data.push(item.value);
            }
        });
        $("#enterpriseData").val(data);
    }

    Array.prototype.contains = function (needle) {
        for (var i in this) {
            if (this[i] == needle) return true;
        }
        return false;
    }
</script>


